package com.example.bingobox.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

public class HealthSelector extends View {
    private Paint gradientPaint;
    private Paint indicatorPaint;
    private Paint textPaint;
    private float indicatorX = -1;
    private int viewWidth;
    private int viewHeight;
    private static final int INDICATOR_WIDTH = 4;
    private static final String[] HEALTH_STATUS = {"健康", "疲惫", "易感", "轻症", "重症"};
    private OnHealthSelectedListener listener;
    private String currentHealth = "";
    private final Rect textBounds = new Rect();

    public HealthSelector(Context context) {
        super(context);
        init();
    }

    public HealthSelector(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        gradientPaint = new Paint();
        gradientPaint.setAntiAlias(true);

        indicatorPaint = new Paint();
        indicatorPaint.setColor(Color.WHITE);
        indicatorPaint.setStrokeWidth(INDICATOR_WIDTH);
        indicatorPaint.setAntiAlias(true);

        textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setAntiAlias(true);
        textPaint.setTextAlign(Paint.Align.CENTER);
        float textSizeInPx = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP,
            12,
            getResources().getDisplayMetrics()
        );
        textPaint.setTextSize(textSizeInPx);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = w;
        viewHeight = h;

        int[] colors = new int[]{
            Color.rgb(50, 205, 50),    // 绿色
            Color.rgb(200, 200, 100),  // 灰黄色
            Color.rgb(200, 150, 100),  // 灰橙色
            Color.rgb(200, 100, 100),  // 灰红色
            Color.rgb(180, 0, 0)       // 深红色
        };

        LinearGradient gradient = new LinearGradient(
            0, 0, viewWidth, 0,
            colors, null,
            Shader.TileMode.CLAMP
        );
        gradientPaint.setShader(gradient);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawRect(0, 0, viewWidth, viewHeight, gradientPaint);

        if (indicatorX >= 0) {
            canvas.drawLine(
                indicatorX, 
                0,  // 从色卡顶部开始
                indicatorX, 
                viewHeight + 20,
                indicatorPaint
            );

            if (!currentHealth.isEmpty()) {
                textPaint.getTextBounds(currentHealth, 0, currentHealth.length(), textBounds);
                float textX = indicatorX;
                float textY = textBounds.height() + 2; // 文字位置在色卡顶部边缘下方一点
                canvas.drawText(currentHealth, textX, textY, textPaint);
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                float x = event.getX();
                updateHealthFromPosition(x);
                return true;
        }
        return super.onTouchEvent(event);
    }

    private void updateHealthFromPosition(float x) {
        x = Math.max(0, Math.min(x, viewWidth));
        indicatorX = x;
        
        int section = (int) (x / (viewWidth / 5.0f));
        section = Math.min(section, 4);
        
        currentHealth = HEALTH_STATUS[section];
        
        if (listener != null) {
            listener.onHealthSelected(currentHealth);
        }
        
        invalidate();
    }

    public void setHealth(String health) {
        if (health == null || health.isEmpty()) {
            indicatorX = -1;
            currentHealth = "";
        } else {
            for (int i = 0; i < HEALTH_STATUS.length; i++) {
                if (HEALTH_STATUS[i].equals(health)) {
                    indicatorX = (viewWidth / 5.0f) * (i + 0.5f);
                    currentHealth = health;
                    break;
                }
            }
        }
        invalidate();
    }

    public interface OnHealthSelectedListener {
        void onHealthSelected(String health);
    }

    public void setOnHealthSelectedListener(OnHealthSelectedListener listener) {
        this.listener = listener;
    }
} 